(require :roll)
;;(require :cl-opengl)

(defvar *manager* (roll/gui:init-gui))
(defvar *win* (roll/gui:make-window :width 300
                                    :height 200
                                    :background :white))

(defvar *gl-canvas* nil)
(defvar *angle* 0.0)
(defun init ()
  (setf *gl-canvas* (roll/gui::make-gl-canvas *win*
                                              :x 0
                                              :y 0
                                              :width 300
                                              :height 200))
  (roll/gui:map-widgets *win*)
  (glx:make-current                     ;cannot call twice... why?
   (roll/gui::xwindow-of *gl-canvas*)
   (roll/gui::gl-context-of *gl-canvas*))
  (gl:ortho 0.0d0 1.0d0 0.0d0 1.0d0 -1.0d0 1.0d0)
  (gl:clear-color 0.0s0 0.0s0 0.0s0 0.0s0)
  (gl:line-width 2.0s0)
  )

(defun test-animation ()
  (unless *gl-canvas*
    (init))
  ;;(roll/util:with-mutex ((roll/gui::mutex-of *manager*))
  (roll/gui::with-gl-make-current-canvas (*gl-canvas*)
    (gl:clear (logior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
    (roll/gui::gl-push-pop-matrix
     (gl:translate-f 0.5s0 0.5s0 0.0s0)
     (gl:rotate-f *angle* 0.0s0 0.0s0 1.0s0)
     (incf *angle* 1.0)
     (gl:translate-f -0.5s0 -0.5s0 0.0s0)
     (roll/gui::gl-block
      gl:+polygon+
      (gl:color-3ub 255 0 0)
      (gl:vertex-2f 0.25s0 0.25s0)
      (gl:color-3ub 0 255 0)
      (gl:vertex-2f 0.75s0 0.25s0)
      (gl:color-3ub 0 0 255)
      (gl:vertex-2f 0.75s0 0.75s0)
      (gl:color-3ub 255 255 255)
      (gl:vertex-2f 0.25s0 0.75s0)
      )
     )
    ;;(glx:wait-x)
    ;;(glx:wait-gl)
    ;;(glx:wait-gl)
    (glx:swap-buffers)
    ;;(glx:wait-gl)
    )
  (glx:wait-gl)
  (sleep 0.001)
  ;;(roll/gui:flush *manager*)
  ;;(roll/gui::xflush)
  )


